// const { request } = require("../../requests");
import regeneratorRuntime from '../../lib/runtime/runtime';
import { request } from "../../requests/index.js";

// pages/goods_list/index.js
Page({
  data: {
    tabs:[
      {
        id:1,
        title:"综合",
        isActive:true

      },{
        id:2,
        title:"销量",
        isActive:false

      },{
        id:3,
        title:"排名",
        isActive:false

      }
    ],
    goodsList :[]
  },
  queryParams:{
    query:"",
    cid:0,
    pagenum:1,
    pagesize:10
  },
  totalNum:0,

  onLoad: function (options) {
    this.queryParams.cid = options.cid;
    this.getGoods();
  },

  async getGoods(){
    const res = await request({url:"/goods/search",data:this.queryParams})
    // console.log(res)
    wx.stopPullDownRefresh();
    this.setData({
      goodsList:[...this.data.goodsList,...res.goods]
    });
    this.totalNum = Math.ceil(res.total / this.queryParams.pagesize);
  },

  handleTabsItemChange(e){
    const tabs = this.data.tabs;
    const index = e.detail.index;
    tabs.forEach((v,i)=>v.id===index? v.isActive=true:v.isActive=false)
    this.setData({
      tabs
    })
  },

  onReachBottom: function () {
    if(this.queryParams.pagenum>=this.totalNum){
      wx.showToast({ title: '没有下一页数据'});
        
    }else{
      this.queryParams.pagenum++;
      this.getGoods()
    }
  },

  onPullDownRefresh: function () {
      this.setData({
        goodsList:[]
      })
      this.queryParams.pagenum=1;
      this.getGoods();
  },

  


 
})